package com.babster631;

import java.util.ArrayList;
import java.util.Random;

import com.babster631folder2.babster631modbase;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.boss.EntityWither;
import net.minecraft.entity.monster.EntityCreeper;
import net.minecraft.entity.monster.EntityPigZombie;
import net.minecraft.entity.monster.EntitySkeleton;
import net.minecraft.entity.monster.EntityWitch;
import net.minecraft.entity.monster.EntityZombie;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemArmor;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;

public class moddedspawner extends Block
{

	public moddedspawner(int par1, Material par2Material) 
	{
		super(par1, par2Material);
		this.setBlockUnbreakable();
		this.setCreativeTab(CreativeTabs.tabBlock);
		// TODO Auto-generated constructor stub
	}
	 public void registerIcons(IconRegister iconReg)
	 {
	        blockIcon = iconReg.registerIcon("babster631modbase:moddedspawner.fw");
	 }
	 @Override
	    public boolean onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9)
	    {
	        activated = true;
	        num_zombie = 0;
	        num_skeleton = 0;
	        par1World.scheduleBlockUpdate(par2, par3, par4, blockID, 1);
	        return false;
	    }
	    
	 
	public void updateTick(World par1World, int par2, int par3, int par4, Random par5random) 
	{
		if(activated == true)
		{
			if (allcreatures.size()< maxcreature && numKilled < killtarget)
			{
				if(num_zombie < maxnumzombie)
	            {
	                    EntityZombie zombie = new EntityZombie(par1World);
	                    Vec3 vec = util.placeInRandomDir(par2, par3, par4, 5);
	                    zombie.setPosition(vec.xCoord, vec.yCoord, vec.zCoord);
	                    par1World.spawnEntityInWorld(zombie);
	                    num_zombie++;
	                    ItemStack ironsword = new ItemStack(Item.swordIron);
	                    ironsword.addEnchantment(Enchantment.sharpness, 1);
	                    ItemStack leatherboots = new ItemStack(ItemArmor.bootsLeather);
	                    leatherboots.addEnchantment(Enchantment.protection, 2);
	                    ItemStack chainlegs = new ItemStack(ItemArmor.legsChain);
	                    chainlegs.addEnchantment(Enchantment.protection, 2);
	                    ItemStack leatherchestplate = new ItemStack(ItemArmor.plateLeather);
	                    leatherchestplate.addEnchantment(Enchantment.protection, 2);
	                    ItemStack chainhelmet = new ItemStack(ItemArmor.helmetChain);
	                    chainhelmet.addEnchantment(Enchantment.protection, 2);
	                    zombie.setCurrentItemOrArmor(0, ironsword);
	                    zombie.setCurrentItemOrArmor(1, leatherboots);
	                    zombie.setCurrentItemOrArmor(2, chainlegs);
	                    zombie.setCurrentItemOrArmor(3, leatherchestplate);
	                    zombie.setCurrentItemOrArmor(4, chainhelmet);
	                    zombie.setChild(true);
	                    zombie.setSprinting(true);
	               
	                    allcreatures.add(zombie);
	                    
	                   
	
	            }
				else if(num_skeleton < maxnumskeleton)
	            {
	                    EntitySkeleton skeleton = new EntitySkeleton(par1World);
	                    Vec3 vec = util.placeInRandomDir(par2, par3, par4, 5);
	                    skeleton.setPosition(vec.xCoord, vec.yCoord, vec.zCoord);
	                    par1World.spawnEntityInWorld(skeleton);
	                    num_skeleton++;
	                    ItemStack bow = new ItemStack(Item.bow);
	                    bow.addEnchantment(Enchantment.power, 1);
	                    ItemStack leatherboots = new ItemStack(ItemArmor.bootsLeather);
	                    leatherboots.addEnchantment(Enchantment.protection, 2);
	                    ItemStack chainlegs = new ItemStack(ItemArmor.legsChain);
	                    chainlegs.addEnchantment(Enchantment.protection, 2);
	                    ItemStack leatherchestplate = new ItemStack(ItemArmor.plateLeather);
	                    leatherchestplate.addEnchantment(Enchantment.protection, 2);
	                    ItemStack chainhelmet = new ItemStack(ItemArmor.helmetChain);
	                    chainhelmet.addEnchantment(Enchantment.protection, 2);
	                    skeleton.setCurrentItemOrArmor(0, bow);
	                    skeleton.setCurrentItemOrArmor(1, leatherboots);
	                    skeleton.setCurrentItemOrArmor(2, chainlegs);
	                    skeleton.setCurrentItemOrArmor(3, leatherchestplate);
	                    skeleton.setCurrentItemOrArmor(4, chainhelmet);
	                    allcreatures.add(skeleton);
	            }
				else if(num_creeper < maxnumcreeper)
	    	    {
	    	            EntityCreeper creeper = new EntityCreeper(par1World);
	    	            Vec3 vec1 = util.placeInRandomDir(par2, par3, par4, 5);
	    	            creeper.setPosition(vec1.xCoord, vec1.yCoord, vec1.zCoord);
	    	            par1World.spawnEntityInWorld(creeper);
	    	            num_creeper++;
	    	            allcreatures.add(creeper);
	    	    }
				else if(num_witch < maxnumwitch)
	    	    {
	    	    		EntityWitch witch = new EntityWitch(par1World);
	    	    		Vec3 vec11 = util.placeInRandomDir(par2, par3, par4, 5);
	    	    		witch.setPosition(vec11.xCoord, vec11.yCoord, vec11.zCoord);
	    	    		par1World.spawnEntityInWorld(witch);
	    	    		num_witch++;
	    	    		allcreatures.add(witch);	  
	    	    }
				
	            }
				else
	    	    {
	    	    	num_zombie = 0;
	    	    	num_skeleton = 0;
	    	    	num_creeper = 0;
	    	    	num_witch = 0;
	    	    	
	    	    	maxnumzombie+=randMobNumber();
	    	    	maxnumskeleton+=randMobNumber();
	    	    	maxnumcreeper+=randMobNumber();
	    	    	maxnumwitch+=randMobNumber();
	    	    	
	    	    	maxcreature+=10;
	    	    }
			}
			else if (numKilled > killtarget && allcreatures.size() == 0)
			{
				 if(num_wither < maxnumwither)
		    	    {
		    	    		EntityWither wither = new EntityWither(par1World);
		    	    		Vec3 vec111 = util.placeInRandomDir(par2, par3, par4, 5);
		    	    		wither.setPosition(vec111.xCoord, vec111.yCoord, vec111.zCoord);
		    	            par1World.spawnEntityInWorld(wither);
		    	            num_wither++;
		    	            allcreatures.add(wither);
		    	            activated = false;

		    	    }
			}
			for( int i = allcreatures.size()-1;i>=0;i--)
	        {
	            if (allcreatures.get(i).isDead)
	            {
	            	if (allcreatures.get(i) instanceof EntityZombie)
	            	{
	            		EntityLiving zombie = allcreatures.get(i);
	            		if (zombie.isChild())
	            		{
	            			allcreatures.get(i).dropItem(babster631modbase.thunderpearl.itemID, 2);
	            		}
	            	}
	                allcreatures.remove(i);
	               
	                numKilled++;
	            }
	        }
	        par1World.scheduleBlockUpdate(par2, par3, par4, blockID, 60);
		}
	
	
	private int randMobNumber()
	{
		int i = (int)(Math.random() * 4);
		return i;
	}
	private int wave = 1;
	private boolean activated = false;
	private int maxcreature = 15;
	private int num_zombie = 0;
	private int num_skeleton = 0;
	private int num_creeper = 0;
	private int num_witch = 0;
	private int num_wither = 0;
	private int maxnumzombie = 5;
	private int maxnumskeleton = 5;
	private int maxnumcreeper = 5;
	private int maxnumwitch = 3;
	private int maxnumwither = 1;
	private int numKilled = 0;
	private int killtarget = 150;


private ArrayList<EntityLiving> allcreatures = new  ArrayList<EntityLiving>();
}
